using System.Collections.Generic;
using System.Web.Routing;
using Nop.Core;
using Nop.Core.Plugins;
using Nop.Services.Cms;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Media;
using Nop.Plugin.Widgets.Ribbon.Data;
using Nop.Plugin.Widgets.Ribbon.Services;
using Nop.Web.Framework.Web;
using Nop.Web.Framework.Menu;
using Nop.Core.Domain.Customers;

namespace Nop.Plugin.Widgets.Ribbon
{
    /// <summary>
    /// PLugin
    /// </summary>
    public class RibbonPlugin : BasePlugin, IWidgetPlugin, IAdminMenuPlugin
    {
        private readonly IPictureService _pictureService;
        private readonly ISettingService _settingService;
        private readonly IWebHelper _webHelper;
        private readonly RibbonObjectContext _objectContext;
        private readonly IRibbonService _ribbonService;
        private readonly ILocalizationService _localizationService;
        private readonly IWorkContext _workContext;

        public RibbonPlugin(IPictureService pictureService, 
            ISettingService settingService, IWebHelper webHelper,
            RibbonObjectContext objectContext, IRibbonService ribbonService,
            ILocalizationService localizationService,IWorkContext workContext)
        {
            this._pictureService = pictureService;
            this._settingService = settingService;
            this._webHelper = webHelper;
            this._objectContext = objectContext;
            this._ribbonService = ribbonService;
            this._localizationService = localizationService;
            this._workContext = workContext;
        }

        /// <summary>
        /// Gets widget zones where this widget should be rendered
        /// </summary>
        /// <returns>Widget zones</returns>
        public IList<string> GetWidgetZones()
        {
            return new string[] {""};
        }

        /// <summary>
        /// Gets a route for provider configuration
        /// </summary>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "Configure";
            controllerName = "WidgetsRibbon";
            routeValues = new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Widgets.Ribbon.Controllers" }, { "area", null } };
        }

        /// <summary>
        /// Gets a route for displaying widget
        /// </summary>
        /// <param name="widgetZone">Widget zone where it's displayed</param>
        /// <param name="actionName">Action name</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="routeValues">Route values</param>
        public void GetDisplayWidgetRoute(string widgetZone, out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = "PublicInfo";
            controllerName = "WidgetsRibbon";
            routeValues = new RouteValueDictionary()
            {
                {"Namespaces", "Nop.Plugin.Widgets.Ribbon.Controllers"},
                {"area", null},
                {"widgetZone", widgetZone}
            };
        }
        
        /// <summary>
        /// Install plugin
        /// </summary>
        public override void Install()
        {
            //data
            _objectContext.Install();
            //configure
            this.AddOrUpdatePluginLocaleResource("plugins.widgets.Ribbon.configure.title", "Ribbon ");
            this.AddOrUpdatePluginLocaleResource("plugins.widgets.Ribbon.configure.message", "Ribbon message");
            this.AddOrUpdatePluginLocaleResource("plugins.widgets.Ribbon.configure.link", "Create Ribbon");

            //list
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Manage", "Manage Ribbons");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.SearchRibbonName", "Ribbon name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.SearchRibbonName.Hint", "Search by ribbon name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Name", "Name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Name.Hint", "Name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Name.Required", "Please provide Name.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Text", "Text");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Text.Hint", "Text");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Text.Required", "Please provide Text.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonClass", "Select Ribbon");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonClass.Hint", "Select Ribbon");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonClass.Required", "Please select ribbon.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonColor", "Select Ribbon color");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonColor.Hint", "Select Ribbon color");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonColor.Required", "Please select ribbon color.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.TargetClass", "Target Class");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.TargetClass.Hint", "Target Class");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.TargetClass.Required", "Please provide a Target Class.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Published", "Published");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Published.Hint", "Published");
           
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Info", "Ribbon Info");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Products", "Ribbon Products");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.SaveBeforeEdit", "You need to save the ribbon.");
            
            //entity list
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.ProductName", "Product Name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.PictureThumbnailUrl", "Picture");
            
            //create
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.AddNew", "Add New Ribbon");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.BackToList", "back to Ribbon list");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.AddNewPicture", "Add new picture");
            //edit
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.EditRibbonDetails", " Edit Ribbon Details");
            
            //other
            this.AddOrUpdatePluginLocaleResource("ActivityLog.EditRibbon", "Edit Ribbon");
            this.AddOrUpdatePluginLocaleResource("ActivityLog.AddNewRibbon", "Ribbon Added");
            this.AddOrUpdatePluginLocaleResource("ActivityLog.DeleteRibbon", "Ribbon Deleted");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Updated", "The Ribbon has been updated successfully.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Added", "The new Ribbon has been added successfully.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.Deleted", "The Ribbon has been deleted successfully.");

            //menu
            this.AddOrUpdatePluginLocaleResource("Plugins.Widgets.Ribbon.AdminMenuText", "Ribbon");

            base.Install();
        }

        /// <summary>
        /// Uninstall plugin
        /// </summary>
        public override void Uninstall()
        {
            //data
            _objectContext.Uninstall();

            //configure
            this.DeletePluginLocaleResource("plugins.widgets.Ribbon.configure.title");
            this.DeletePluginLocaleResource("plugins.widgets.Ribbon.configure.message");
            this.DeletePluginLocaleResource("plugins.widgets.Ribbon.configure.link");

            //list
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Manage");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.SearchRibbonName");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.SearchRibbonName.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Name");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Name.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Name.Required");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Text");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Text.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Text.Required");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonClass");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonClass.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonClass.Required");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonColor");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonColor.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.RibbonColor.Required");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.TargetClass");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.TargetClass.Hint");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.TargetClass.Required");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Published");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.Published.Hint");

            //entity list
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.ProductName");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Fields.PictureThumbnailUrl");
            
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Info");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Products");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.PreviewMessage");

            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.SaveBeforeEdit");

            //create
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.AddNew");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.BackToList");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.AddNewPicture");
            
            //edit
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.EditRibbonDetails");
            
            //other
            this.DeletePluginLocaleResource("ActivityLog.EditRibbon");
            this.DeletePluginLocaleResource("ActivityLog.AddNewRibbon");
            this.DeletePluginLocaleResource("ActivityLog.DeleteRibbon");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Updated");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Added");
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.Deleted");
            //admin menu
            this.DeletePluginLocaleResource("Plugins.Widgets.Ribbon.AdminMenuText");
            base.Uninstall();
        }

        // Implement an admin link
        public bool Authenticate()
        {
            if (_workContext.CurrentCustomer.IsAdmin())
                return true;
            else
                return false;
        }

        public SiteMapNode BuildMenuItem()
        {
            var menuItem = new SiteMapNode()
            {
                Title = _localizationService.GetResource("Plugins.Widgets.Ribbon.AdminMenuText"),
                ControllerName = "WidgetsRibbon",
                ActionName = "List",
                Visible = true,
                RouteValues = new RouteValueDictionary() { { "area", null } },
            };
            return menuItem;

        }
        
    }
}
